package gui;

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
/*
 * This class represent the visual gameboard in four in a row
 * Initializes a new label matrix with the same amount of columns and rows
 * as the gameGrid class.
 */

public class GameBoard extends JPanel {

    private Label[][] label;
    private int column;
    private int row;
    private GUIController controller;
    private final Icon EMPTY = loadBoardPictures("src\\gui\\images\\4row_board.png");
    private final Icon RED = loadBoardPictures("src\\gui\\images\\4row_redBoard.jpg");
    private final Icon BLACK = loadBoardPictures("src\\gui\\images\\4row_blackBoard.jpg");
    /*
     * GameBoard constructor.
     * Creates a new label with the same rows and columns as the game grid.
     */

    public GameBoard(GUIController controller) {
        this.controller = controller;
        column = controller.getGameManager().getGameGrid().COLUMNS;
        row = controller.getGameManager().getGameGrid().ROWS;
        label = new Label[row][column];
        createBoard();
    }
    /*
     * This method creates the game board
     * by adding a new label to each cell in the grid.
     */

    private void createBoard() {;
        setLayout(new GridLayout(row, column));
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                label[i][j] = new Label(j);
                add(label[i][j]);
            }
        }
    }

    public void resetGameBoard() {
        for (int i = row - 1; i >= 0; i--) {
            for (int j = column - 1; j >= 0; j--) {
                label[i][j].setIcon(EMPTY);
            }
        }
        repaint();
    }
    /*
     * This method takes the game grid and updates the game board 
     * and adds an image to the cell that has an 0 for a board image, 
     * 1 for a red image, and 2 for a black image.
     */

    public void updateBoard() {
        int[][] grid = controller.getGameManager().getGrid();

        for (int i = row - 1; i >= 0; i--) {
            for (int j = column - 1; j >= 0; j--) {
                if (grid[i][j] == 1) {
                    label[i][j].setIcon(BLACK);
                } else if (grid[i][j] == 2) {
                    label[i][j].setIcon(RED);
                } else {
                    label[i][j].setIcon(EMPTY);
                }
            }
        }
        repaint();
    }

    private Icon loadBoardPictures(String str) {
        BufferedImage myPicture = null;

        try {
            myPicture = ImageIO.read(new File(str));
            return new ImageIcon(myPicture);
        } catch (IOException ex) {
            controller.showErrorDialog("Error while loading image " + str);
        }
        return null;
    }
    /*
     * This class extends JLabels.
     * Adds an image icon of a 4 in a row board 
     * and a mouse listener to each label.
     */

    public class Label extends JLabel {

        private int column;

        public Label(int column) {
            this.column = column;
            LabelMouseListener listener = new LabelMouseListener(column);
            addMouseListener(listener);
            setIcon(EMPTY);
        }

        public int getColumn() {
            return column;
        }
    }
    /*
     * This class extends MouseAdapter.
     */

    public class LabelMouseListener extends MouseAdapter implements MouseListener {

        private int column;

        public LabelMouseListener(int column) {
            super();
            this.column = column;
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            controller.getGameManager().getMarkerQueue().addColumnToQueue(column);
        }
    }
}